package com.jp.knowledge.adapter;


import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.dialog.DelResonDialog;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.HeadLineData;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.util.L;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.util.TimeUtil;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;


/**
 * Created by jiangyue on 2016/4/12.
 */
public class CommtemAdapterold extends JPBaseAdpter implements View.OnClickListener, ResultSubscriber.OnResultListener {
    private ListView curListView;

    private DelResonDialog delDialog;
    private View shadeView;


    private int curPostion;

    public CommtemAdapterold(Context context, List infos) {
        super(context, infos);
//        shadeView = ((JpMainActivity)context).getShadeView();
//        delDialog.setShadeView(shadeView);
        delDialog = new DelResonDialog((Activity) context);

        delDialog.setDelDone(new DelResonDialog.DelDone() {
            @Override
            public void done(int type) {
                int postion = curPostion;
                HeadLineData headLineData = (HeadLineData) getItem(postion);
                JsonObject param = getParams(headLineData, type);
                HttpHelper.getInstance(getContext()).closeHeadContent(param, postion, CommtemAdapterold.this);
            }
        });
    }

    public void setCurListView(ListView curListView) {
        this.curListView = curListView;
    }

    @Override
    protected int getLayoutId(int position) {
        return R.layout.comm_item_layout;
    }

    @Override
    protected void renderData(JPViewHolder holder, Object item, int position) {
        HeadLineData headLineData = (HeadLineData) item;
        TextView title = holder.getView(R.id.description);
        title.setText(headLineData.getTitle());//设置名称

        if (headLineData.isLook()) {
            title.setTextColor(getContext().getResources().getColor(R.color.gray_light));
        } else {
            title.setTextColor(getContext().getResources().getColor(R.color.black));
        }

        holder.setText(R.id.type, headLineData.getLabel());//类型
        holder.setText(R.id.author, headLineData.getSource());//作者
//        String time = TimeUtil.getStandard(headLineData.getCreatetime());
        holder.setText(R.id.time, TimeUtil.friendly_time(headLineData.getCreatetime()));//创建时间
        ImageView photo = holder.getView(R.id.photo);
        ImageView iconClose = holder.getView(R.id.icon_close);
        ImageView icon = holder.getView(R.id.desc_icon);
        ImageView icon_1 = holder.getView(R.id.desc_icon_1);
        ImageView icon_2 = holder.getView(R.id.desc_icon_2);
        ImageView icon_3 = holder.getView(R.id.desc_icon_3);
        ImageView[] imgs = new ImageView[3];
        imgs[0] = icon_1;
        imgs[1] = icon_2;
        imgs[2] = icon_3;
        List<HeadLineData.ImglistBean> list = headLineData.getImglist();
        LinearLayout muiltIcon = holder.getView(R.id.muilt_icon);
        Glide.with(getContext()).load(headLineData.getIcon()).bitmapTransform(new CropCircleTransformation(getContext())).override(30, 30).into(photo);//new BlurTransformation(getContext(), 25),
        if (list != null) {
            int size = list.size();
            if (size == 1) {
                icon.setVisibility(View.VISIBLE);
                String url = list.get(0).getSrc();
                Glide.with(getContext()).load(url).into(icon);
                muiltIcon.setVisibility(View.GONE);
            } else if (size > 1) {
                muiltIcon.setVisibility(View.VISIBLE);
                icon.setVisibility(View.GONE);
                for (int i = 0; i < size; i++) {
                    if (i < 3) {
                        Glide.with(getContext()).load(list.get(i).getSrc()).into(imgs[i]);
                    }

                }
            }

        }
        iconClose.setTag(position);
        iconClose.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        curPostion = (int) view.getTag();
        int[] localtion = new int[2];
        view.getLocationOnScreen(localtion);
        int x = localtion[0]+view.getWidth();
        int y = localtion[1];
        delDialog.show(x, y - 20);

    }

    private JsonObject getParams(HeadLineData headLineData, int type) {
        JsonObject obj = new JsonObject();
        obj.addProperty("id", headLineData.getId());
        obj.addProperty("type", headLineData.getType());
        obj.addProperty("closeType", type);
        return obj;
    }

    @Override
    public void onStart(int requestType) {

    }

    @Override
    public void onCompleted(int requestType) {

    }

    @Override
    public void onError(int requestType) {

    }

    @Override
    public void onNext(IModel data, int requestType) {
        int success = data.getErrcode();
        if (success == 0) {
            if (delDialog.isShowing()) {
                delDialog.dismiss();
//                shadeView.setVisibility(View.GONE);
            }

//            if(curListView!=null)curListView.removeViewAt(requestType);
            removeItem(requestType);
//            deletePattern(curListView.getChildAt(requestType),requestType);
        }
    }

    /**
     * 改变看过的内容颜色
     *
     * @param position
     */
    public void updateTitleColor(int position) {
        HeadLineData headLineData = (HeadLineData) mInfos.get(position);
        headLineData.setLook(true);
        notifyDataSetChanged();
    }



    private void deletePattern(final View view, final int position) {

        Animation.AnimationListener al = new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                mInfos.remove(position);
//                CommtemAdapter.this.notifyDataSetChanged();
//                removeItem(position);
                L.e("onAnimationEnd-----onAnimationEnd=");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        };
        collapse(view, al,position);

    }

    private void collapse(final View view, Animation.AnimationListener al,final  int position) {
        if(view!=null){


        final int originHeight = view.getMeasuredHeight();

        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if (interpolatedTime == 1.0f) {
                    view.setVisibility(View.GONE);
                    L.e("applyTransformation-----interpolatedTime="+interpolatedTime);
//                    removeItem(position);
//                    curListView.removeView(view);
                } else {
                    int heigth = originHeight - (int) (originHeight * interpolatedTime);
                    view.getLayoutParams().height =heigth;
                    L.e("applyTransformation-----height="+heigth);
                    view.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        if (al != null) {
            animation.setAnimationListener(al);
        }
        animation.setDuration(300);
        view.startAnimation(animation);
        }
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                handler.sendEmptyMessage(position);
//            }
//        },500);

    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            removeItem(msg.what);
        }
    };
}
